//
//  ZZFatherView.swift
//  网易微博
//
//  Created by gazyy1 on 16/6/25.
//  Copyright © 2016年 gazyy1. All rights reserved.
//

import UIKit
import SnapKit

class ZZFatherView: UIView {
    

    //圆环图片
    lazy var feedImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮罩图片
    lazy var hideImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //房子图片
    lazy var homeImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //显示信息label
    lazy var messageLabel : UILabel = {
       
        let label = UILabel()
        label.textAlignment = .Center
        label.textColor = TemColor
        label.text = "关注一些人,回这里看看有什么惊喜关注一些人,回这里看看有什么惊喜"
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        return label
        
    }()
    //登陆按钮
    lazy var loginButton : UIButton = {
        
        let button = UIButton()
        button.setTitle("登陆", forState: UIControlState.Normal)
        button.setTitleColor(TemColor, forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
       return button
    }()
    //注册按钮
    lazy var newButton : UIButton = {
        
        let button = UIButton()
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(TemColor, forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()

    
    //定义闭包
    var buttonBlock : (() -> ())?
    
    @objc private func buttonClick(){
        
        buttonBlock?()
    }
    
    
    
    
    //主函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //布局控件
        setupUI()
        

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        //添加控件
        addSubview(feedImageView)
        addSubview(hideImageView)
        addSubview(homeImageView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(newButton)
        
        //设置约束
        feedImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
            
        }
        hideImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        homeImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        //label约束
        messageLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(hideImageView.snp_bottom).offset(16)
            make.centerX.equalTo(self)
            make.width.equalTo(224)
            make.height.equalTo(36)
        }
        //button设置约束
        loginButton.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.height.equalTo(36)
            make.width.equalTo(100)
        }
        newButton.snp_makeConstraints { (make) -> Void in
            
            make.right.equalTo(messageLabel)
            make.top.equalTo(loginButton)
            make.height.equalTo(36)
            make.width.equalTo(100)
        }
        
        
    }
    
    
    
    //方法
    func setFatherVC(imageName : String?,title : String?){
        
        if let img = imageName,let tit = title {
            
            homeImageView.image = UIImage(named: img)
            messageLabel.text = tit
            
            feedImageView.hidden = true
            
        }else{
            //这里显示主页 设置动画
            setAni()
        }
        
        
    }
    
    
    private func setAni(){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = M_PI * 2
        anim.duration = 10
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        
        feedImageView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    
    
}
